package demo1;

import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    public int maxCoins(int[] piles) {
        Arrays.sort(piles);
        int left = 0, right = piles.length - 1;
        int result = 0;
        while (left < right) {
            left++;
            result += piles[--right];
            right--;
        }
        return result;
    }

    public int shortestPath(int node1, int node2) {
        int n = adjacencyMatrix.length;
        int[] distance = new int[n];
        boolean[] visited = new boolean[n];

        Arrays.fill(distance, Integer.MAX_VALUE);
        distance[node1] = 0;

        PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
        pq.offer(new int[]{node1, 0});

        while (!pq.isEmpty()) {
            int[] curr = pq.poll();
            int currNode = curr[0];
            int currDist = curr[1];

            if (visited[currNode]) continue;
            visited[currNode] = true;

            if (currNode == node2) return currDist;

            if (adjacencyList.containsKey(currNode)) {
                for (int[] neighbor : adjacencyList.get(currNode)) {
                    int nextNode = neighbor[0];
                    int edgeCost = neighbor[1];

                    if (!visited[nextNode] && currDist + edgeCost < distance[nextNode]) {
                        distance[nextNode] = currDist + edgeCost;
                        pq.offer(new int[]{nextNode, distance[nextNode]});
                    }
                }
            }
        }

        return -1;
    }
}
